var express = require('express');
var router = express.Router();

var md5 = require("md5");
var fs=require("fs");
var conn = require('../conn');
var cart = require('../cart');
/* GET home page. */
router.get('/', function(req, res, next) {
  var sql = "select * from product where cata_id=1 limit 8";
  conn.query(sql,function(err,rows,fields){
    var flower=rows;
    conn.query("select * from product where cata_id=3 limit 8",function(err,rows,fields){
      var zhang=rows;
      conn.query("select * from product where type_id=2 limit 8",function(err,rows,fields){
       var yong=rows;
        conn.query("select * from product where type_id=3 limit 8",function(err,rows,fields){
          var cake=rows;
          conn.query("select * from product where type_id=4 limit 8",function(err,rows,fields){
            var gift=rows;
            var  islogin;
            if(req.session.islogin==true){
                islogin = "<li><a href='/member/MemberCenter'>欢迎"+req.session.username+"</a></li><li><a href='/loginOut'>退出</a>&nbsp;<b>|</b>&nbsp;<a href='/member/myOrder'>订单查询</a></li>"
            }else{
                islogin  = '<li><a href="/login">你好，请登录</a></li> <li><a href="/register">注册</a></li>'
            }
              res.render('index', {flower: flower,zhang:zhang,yong:yong, cake:cake,gift:gift,shopping_num:req.session.shopping_num,islogin:islogin});
            })
          })
        })
      });
    });
  });

/*鲜花模块*/
router.get('/flower', function(req, res, next) {
  conn.query("select count(*) as c from product where type_id=1",function(err,rows,fields){
    req.flower_num = rows[0].c;
    next();
  });
},function(req, res, next){
  if (req.query.page == undefined) {
    var page=1
  }else{
    page = req.query.page;
  }
  var pageSize = 8;
  var pageLength = Math.ceil(req.flower_num/pageSize);
  var sql = "select * from product where type_id=1 limit "+(page-1)*pageSize+","+pageSize;
  conn.query("select * from producttype",function(err,rows,fields){
    var type = rows;
    conn.query(sql,function(err,rows,fields){
      var urlList = req.url.substr(0,5);
      var  islogin;
            if(req.session.islogin==true){
                islogin = "<li><a href='/member/MemberCenter'>欢迎"+req.session.username+"</a></li><li><a href='/loginOut'>退出</a>&nbsp;<b>|</b>&nbsp;<a href='/member/myOrder'>订单查询</a></li>"
            }else{
                islogin  = '<li><a href="/login">你好，请登录</a></li> <li><a href="/register">注册</a></li>'
            }
      res.render('flower', { flower: rows,type:type,url:urlList,page:pageLength,page_id:page-1 ,flower_num:req.flower_num,shopping_num:req.session.shopping_num,islogin:islogin});
    })
  })
});

router.get('/flowerList', function(req, res, next) {
  conn.query("select count(*) as c from product where cata_id="+req.query.cata_id,function(err,rows,fields){
    req.flower_num = rows[0].c;
    next();
  });
},function(req, res, next){
  if (req.query.page == undefined) {
    var page=1
  }else{
    page = req.query.page;
  }
  var pageSize = 8;
  var pageLength = Math.ceil(req.flower_num/pageSize);
  var sql = "select * from product where cata_id="+req.query.cata_id+" limit "+(page-1)*pageSize+","+pageSize;
  conn.query("select * from producttype",function(err,rows,fields){
    var type = rows;
    conn.query(sql,function(err,rows,fields){
      var urlList = req.url.substr(0,5);
      var  islogin;
            if(req.session.islogin==true){
                islogin = "<li><a href='/member/MemberCenter'>欢迎"+req.session.username+"</a></li><li><a href='/loginOut'>退出</a>&nbsp;<b>|</b>&nbsp;<a href='/member/myOrder'>订单查询</a></li>"
            }else{
                islogin  = '<li><a href="/login">你好，请登录</a></li> <li><a href="/register">注册</a></li>'
            }
      res.render('flowerList', { flower: rows ,type:type,url:urlList,page:pageLength,page_id:page-1,flower_num:req.flower_num,cata_id:req.query.cata_id,shopping_num:req.session.shopping_num,islogin:islogin});
    })
  })
});

/*永生花模块*/
router.get('/yongshenghua', function(req, res, next) {
  var sql = "select * from product where cata_id=6 limit 3";
  conn.query(sql,function(err,rows,fields){
    var yong_box=rows;
    conn.query("select * from product where cata_id=7 limit 3",function(err,rows,fields){
      var yong_large=rows;
      conn.query("select * from product where cata_id=8 limit 3",function(err,rows,fields){
       var yong_xun=rows;
        conn.query("select * from product where cata_id=9 limit 3",function(err,rows,fields){
          var yong_vase=rows;
          conn.query("select * from product where cata_id=10 limit 3",function(err,rows,fields){
            var yong_te=rows;
            conn.query("select * from producttype",function(err,rows,fields){
              var type = rows;
              var urlList = req.url.substr(0,5);
              var  islogin;
            if(req.session.islogin==true){
                islogin = "<li><a href='/member/MemberCenter'>欢迎"+req.session.username+"</a></li><li><a href='/loginOut'>退出</a>&nbsp;<b>|</b>&nbsp;<a href='/member/myOrder'>订单查询</a></li>"
            }else{
                islogin  = '<li><a href="/login">你好，请登录</a></li> <li><a href="/register">注册</a></li>'
            }
              res.render('yongshenghua', { type:type,url:urlList,yong_box: yong_box,yong_large:yong_large,yong_xun:yong_xun, yong_vase:yong_vase,yong_te:yong_te,shopping_num:req.session.shopping_num,islogin:islogin});
            })
          })
        })
      })
    });
  });
});
router.get('/yongshenghuaList', function(req, res, next) {
  conn.query("select * from catalog where ptype_id=2",function(err,rows,fields){
    var cata = rows;
    var sql = "select * from product where cata_id="+req.query.cata_id;
    conn.query("select * from producttype",function(err,rows,fields){
      var type = rows;
      conn.query(sql,function(err,rows,fields){
        var urlList = req.url.substr(0,5);
        res.render('yongshenghuaList', { item: rows ,type:type,url:urlList,cata:cata,cata_id:req.query.cata_id});
      })
    })
  })
});
/*蛋糕模块*/
router.get('/cake', function(req, res, next) {
  var sql = "select * from product where cata_id=20 limit 4";
  conn.query(sql,function(err,rows,fields){
    var yuanzu=rows;
    conn.query("select * from product where cata_id=21 limit 4",function(err,rows,fields){
      var haoli=rows;
      conn.query("select * from product where cata_id=22 limit 4",function(err,rows,fields){
        var xingfu=rows;
        conn.query("select * from producttype",function(err,rows,fields){
          var type = rows;
          var urlList = req.url.substr(0,5);
          var  islogin;
            if(req.session.islogin==true){
                islogin = "<li><a href='/member/MemberCenter'>欢迎"+req.session.username+"</a></li><li><a href='/loginOut'>退出</a>&nbsp;<b>|</b>&nbsp;<a href='/member/myOrder'>订单查询</a></li>"
            }else{
                islogin  = '<li><a href="/login">你好，请登录</a></li> <li><a href="/register">注册</a></li>'
            }
          res.render('cake', { yuanzu: yuanzu,type:type,url:urlList,haoli:haoli, xingfu:xingfu,shopping_num:req.session.shopping_num,islogin:islogin});
        })
      })
    });
  });
});
router.get('/cakeList', function(req, res, next) {
  conn.query("select count(*) as c from product where cata_id="+req.query.cata_id,function(err,rows,fields){
    req.cake_num = rows[0].c;
    next();
  });
},function(req, res, next){
  if (req.query.page == undefined) {
    var page=1
  }else{
    page = req.query.page;
  }
  var pageSize = 8;
  var pageLength = Math.ceil(req.cake_num/pageSize);
  if (req.query.p == '1') {
    var sql = "select * from product where cata_id="+req.query.cata_id+" order by price limit "+(page-1)*pageSize+","+pageSize;
  }else if (req.query.p == '2') {
    var sql = "select * from product where cata_id="+req.query.cata_id+" order by price desc limit "+(page-1)*pageSize+","+pageSize;
  }else{
    var sql = "select * from product where cata_id="+req.query.cata_id+" limit "+(page-1)*pageSize+","+pageSize;
  }
  conn.query("select * from producttype",function(err,rows,fields){
    var type = rows;
    conn.query(sql,function(err,rows,fields){
      var urlList = req.url.substr(0,5);
      res.render('cakeList', { cake: rows ,type:type,url:urlList,page:pageLength,page_id:page-1,cake_num:req.cake_num,cata_id:req.query.cata_id,pr:req.query.p});
    })
  });
});

/*礼品模块*/
router.get('/gifts', function(req, res, next) {
  var sql = "select * from product where cata_id=24 limit 8";
  conn.query(sql,function(err,rows,fields){
    var gift_tg=rows;
    conn.query("select * from product where cata_id=25 limit 4",function(err,rows,fields){
      var gift_music=rows;
      conn.query("select * from product where cata_id=26 limit 4",function(err,rows,fields){
       var gift_shui=rows;
        conn.query("select * from product where cata_id=27 limit 4",function(err,rows,fields){
          var gift_glod=rows;
          conn.query("select * from producttype",function(err,rows,fields){
            var type = rows;
            var urlList = req.url.substr(0,5);
            if(req.session.islogin==true){
                islogin = "<li><a href='/member/MemberCenter'>欢迎"+req.session.username+"</a></li><li><a href='/loginOut'>退出</a>&nbsp;<b>|</b>&nbsp;<a href='/member/myOrder'>订单查询</a></li>"
            }else{
                islogin  = '<li><a href="/login">你好，请登录</a></li> <li><a href="/register">注册</a></li>'
            }
            res.render('gifts', { type:type,url:urlList,gift_tg: gift_tg,gift_music:gift_music,gift_shui:gift_shui, gift_glod:gift_glod,shopping_num:req.session.shopping_num,islogin:islogin});
          })
        })
      })
    });
  });
});
router.get('/giftsList', function(req, res, next) {
  conn.query("select count(*) as c from product where cata_id="+req.query.cata_id,function(err,rows,fields){
    req.cake_num = rows[0].c;
    next();
  });
},function(req, res, next){
  if (req.query.page == undefined) {
    var page=1
  }else{
    page = req.query.page;
  }
  var pageSize = 12;
  var pageLength = Math.ceil(req.cake_num/pageSize);
  if (req.query.p == '1') {
    var sql = "select * from product where cata_id="+req.query.cata_id+" order by price limit "+(page-1)*pageSize+","+pageSize;
  }else if (req.query.p == '2') {
    var sql = "select * from product where cata_id="+req.query.cata_id+" order by price desc limit "+(page-1)*pageSize+","+pageSize;
  }else{
    var sql = "select * from product where cata_id="+req.query.cata_id+" limit "+(page-1)*pageSize+","+pageSize;
  }
  conn.query("select * from producttype",function(err,rows,fields){
    var type = rows;
    conn.query(sql,function(err,rows,fields){
      var urlList = req.url.substr(0,5);
      res.render('giftsList', { gift: rows , type:type,url:urlList,page:pageLength,page_id:page-1,cake_num:req.cake_num,cata_id:req.query.cata_id,pr:req.query.p});
    })
  });
});
/*巧克力模块*/
router.get('/chocolates', function(req, res, next) {
  var sql = "select * from product where cata_id=16";
  conn.query(sql,function(err,rows,fields){
    var cho_jin=rows;
    conn.query("select * from product where cata_id=17",function(err,rows,fields){
      var cho_q=rows;
      conn.query("select * from product where cata_id=18",function(err,rows,fields){
       var cho_r=rows;
        conn.query("select * from product where cata_id=19",function(err,rows,fields){
          var cho_s=rows;
          conn.query("select * from producttype",function(err,rows,fields){
            var type = rows;
            var urlList = req.url.substr(0,5);
            var  islogin;
            if(req.session.islogin==true){
                islogin = "<li><a href='/member/MemberCenter'>欢迎"+req.session.username+"</a></li><li><a href='/loginOut'>退出</a>&nbsp;<b>|</b>&nbsp;<a href='/member/myOrder'>订单查询</a></li>"
            }else{
                islogin  = '<li><a href="/login">你好，请登录</a></li> <li><a href="/register">注册</a></li>'
            }
            res.render('chocolates', { type:type,url:urlList,cho_jin: cho_jin,cho_q:cho_q,cho_r:cho_r, cho_s:cho_s,shopping_num:req.session.shopping_num,islogin:islogin});
          })
        })
      })
    });
  });
});

/*---------用户登录注册-----------*/
router.get('/login', function(req, res, next) {
  var  islogin; 
  islogin  = '<li><a href="/login">你好，请登录</a></li> <li><a href="/register">注册</a></li>'
        
  res.render('login',{shopping_num:req.session.shopping_num,islogin:islogin})
});
router.post('/dologin',function(req, res, next){
  req.body.password=md5(req.body.password);
  var sql='select * from users where username="'+req.body.username+'" and password="'+req.body.password+'"';
  conn.query(sql,function(err, rows, fields){
    if(rows.length==0){
      res.send('账号或密码不正确')
    }else{
      req.session.islogin=true;
      req.session.username=req.body.username;

      for(var i = 0 ;i < rows.length ;i++){
          if (rows[i].username == req.body.username) {
            req.session.user_id=rows[i].id;
          }
      }
      req.session.password=req.body.password;
      res.send('ok');
    }
  })
})
router.get('/register', function(req, res, next) {
  var  islogin; 
  islogin  = '<li><a href="/login">你好，请登录</a></li> <li><a href="/register">注册</a></li>'
  res.render('register',{shopping_num:req.session.shopping_num,islogin:islogin})
});
router.post('/doregister',function(req, res, next){
  req.body.password=md5(req.body.password);
  req.body.password_again=md5(req.body.password_again);
  var sql='insert into users values(null,"'+req.body.username+'","'+req.body.password+'","'+req.body.password_again+'")';
    if(req.body.identify!=req.body.codese) {
     res.send('您输入的验证码不正确')
    }else if(req.body.password_again!=req.body.password) {
       res.send('两次输入密码不一致')
    }else if(req.body.username==0){
       res.send('用户名不能为空！')
    }else{
      req.session.islogin=true;
      req.session.username=req.body.username;
      conn.query(sql,function(err, rows, fields){   
        res.send("ok");     
      })
    }
 })
router.get('/loginOut', function(req, res, next) {
  req.session.destroy();
  res.redirect('login')
});
/*-----------商品购买-详情页------------*/
router.get('/buy', function(req, res, next) {
  
   if (req.session.islogin==true) {
    conn.query('select count(*) from shopping where user_id='+req.session.user_id+'',function(err, rows, fields){
      req.shopping_num = rows[0]['count(*)'];
      req.session.shopping_num=req.shopping_num;
       var sql='select * from product where id='+req.query.id;
        conn.query(sql,function(err, rows, fields){
           islogin = "<li><a href='/member/MemberCenter'>欢迎"+req.session.username+"</a></li><li><a href='/loginOut'>退出</a>&nbsp;<b>|</b>&nbsp;<a href='/member/myOrder'>订单查询</a></li>"
           req.session.islogins='<a href="/fillOrder?id='+rows[0].id+'" class="btn btn-primary btn-lg" id="Btn_AddToCart"><span class="ico ico-cart-white"></span>立即购买</a>'
          res.render('buy',{buy:rows[0],shopping_num:req.session.shopping_num,islogins:req.session.islogins,islogin:islogin})
      })
    })
   }else{
       var sql='select * from product where id='+req.query.id;
        conn.query(sql,function(err, rows, fields){
          islogin  = '<li><a href="/login">你好，请登录</a></li> <li><a href="/register">注册</a></li>'
          req.session.islogins='<a href="/login" class="btn btn-primary btn-lg" id="Btn_AddToCart"><span class="ico ico-cart-white"></span>立即购买</a>'
          res.render('buy',{buy:rows[0],shopping_num:0,islogins:req.session.islogins,islogin:islogin})
      })
      
     
  }
});
/*------------订单页面----------------*/
router.get('/fillOrder', function(req, res, next) {
   var sql='select * from product where id='+req.query.id;
    conn.query(sql,function(err, rows, fields){   
      var islogin;
      if(req.session.islogin==true){
          islogin = "<li><a href='/member/MemberCenter'>欢迎"+req.session.username+"</a></li><li><a href='/loginOut'>退出</a>&nbsp;<b>|</b>&nbsp;<a href='/member/myOrder'>订单查询</a></li>"
      }else{
          islogin  = '<li><a href="/login">你好，请登录</a></li> <li><a href="/register">注册</a></li>'
      }
      res.render('fillOrder',{buy:rows[0],shopping_num:req.session.shopping_num,islogin:islogin})
    })
});
router.post('/dofillOrder',function(req, res, next){
  var sql='insert into order_infro values(null,"'+req.body.receiver+'","'+req.body.amount+'","'+req.body.deliveryTime+'","'+req.body.allState+'","'+req.session.user_id+'")';
    conn.query(sql,function(err, rows, fields){   
      console.log(sql)
      res.send("下单成功！");
    })
 })
/*-----------购物车------------*/
router.get('/shopping', function(req, res, next) {
  cart.cartList(req, res, next,conn)
});

//添加购物车
router.post('/doshopping',function(req, res, next){
    cart.cartAdd(req, res, next,conn)
})



router.get('/delshopping', function(req, res, next) {
  conn.query('delete from shopping where id='+req.query.id, function(err, rows, fields){
  res.redirect("shopping");
  })
});
router.get('/del_all_shopping', function(req, res, next) {
  conn.query('delete from shopping', function(err, rows, fields){

  res.redirect("shopping");
  })
});






router.get('/Product', function(req, res, next) {
  res.render('buy', { title: 'Express' });
});

router.get('/Passport/Login', function(req, res, next) {
  res.render('Login');
});
router.get('/Passport/Register', function(req, res, next) {
  res.render('Register');
});

router.get('/shopping/shopcar', function(req, res, next) {
  res.render('shopcar');
});


/*测试*/
router.get('/tjg', function(req, res, next) {
  conn.query("select * from producttype",function(err,rows,fields){
    res.render('tjg', {type:rows});
  })
});


module.exports = router;
